package com.planw.beetl.findUsages

import com.intellij.patterns.PlatformPatterns
import com.intellij.psi.*
import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.util.ProcessingContext
import org.intellij.plugins.markdown.lang.psi.impl.MarkdownHeaderImpl

open class BeetlSqlIdPsiReferenceContributor : PsiReferenceContributor() {
    override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
        registrar.registerReferenceProvider(PlatformPatterns.psiElement(LeafPsiElement::class.java), object : PsiReferenceProvider() {
            override fun getReferencesByElement(element: PsiElement, context: ProcessingContext): Array<PsiReference> {
                val methods = BeetlSqlFindUsagesProvider.findUsagesMapperMethod(element)
                if(methods != null){
                    val list = ReferencesSearch.search(methods[0]).findAll() as List<*>;
                    if(list != null && list.isNotEmpty()){
                        return  list.toTypedArray() as Array<PsiReference>
                    }
                }
                return PsiReference.EMPTY_ARRAY
            }
        })
    }
}